﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks.Dataflow;

namespace SearchAlgorithms.Services
{
    public class IntegrateBlock
    {

        private readonly List<byte> accumulatedFrames = new List<byte>(); // 用于积累帧数据的缓存区

        public IntegrateBlock()
        {
            

            _Process = new ActionBlock<(byte[], int)>(buffer =>
            {
                accumulatedFrames.AddRange(buffer.Item1);
                

                if (buffer.Item2 == 1)
                {
                    byte[] integratedFrame = accumulatedFrames.ToArray();
                    accumulatedFrames.Clear();
                    DataArrived?.Invoke(integratedFrame);
                }
            });
        }

        public void Enqueue((byte[],int) buffer)
        {
            _Process.Post(buffer);
            
        }

        public ActionBlock<(byte[], int)> _Process;

        public Action<byte[]> DataArrived;

    }
}
